<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Response;
use PHPExcel_IOFactory;
use PHPExcel;

class Index extends Controller
{
    public function index()
    {
          //获取实验测试数据
        $formData=array('start_time'=>'','end_time'=>'','searchtype'=>'','keyword'=>'');
        
        $formData['start_time']=input('start_time');
        $formData['end_time']=input('end_time');
        $formData['searchtype']=input('searchtype');
        $formData['keyword']=input('keyword');
        $experiment=model('index/experiment');
        $dataList=$experiment->dataList();
        $this->assign('dataList',$dataList);
        $this->assign('data',$formData);
        $this->assign('page',$dataList->render());
         return view('index');
    }
    //去提交数据
    public function toCommit(){
        return view('commit');
    }
    //提交测量数据
    public function commitData()
    {
        $experiment=model('index/experiment');
        $rs=$experiment->commitData();
    }
    //图片上传
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(  'uploads');
           
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
             //   echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
               // echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
               // echo $info->getFilename(); 
               echo json_encode(array('errcode'=>0,'filePath'=>'uploads/'.$info->getSaveName()));
               exit;
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }

    //对记录进行删除
    public function del(){
        $id=input('id');
        $experiment=model('index/experiment');
        $rs=$experiment->del($id);
        if($rs){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //导出excel表格
    public function exportExcel(){
        $path = dirname(__FILE__); //找到当前脚本所在路径
        $PHPExcel = new PHPExcel(); //实例化PHPExcel类，类似于在桌面上新建一个Excel表格
        $PHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $PHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $PHPSheet = $PHPExcel->getActiveSheet(); //获得当前活动sheet的操作对象
        $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
       
        $PHPSheet->getColumnDimension('A')->setWidth(20);
        $PHPSheet->getColumnDimension('B')->setWidth(30);
        $PHPSheet->getColumnDimension('C')->setWidth(10);
        $PHPSheet->getColumnDimension('D')->setAutoSize(true);
        $PHPSheet->getColumnDimension('E')->setWidth(80);
       
        $PHPSheet->getColumnDimension('F')->setAutoSize(true);
        $PHPSheet->setCellValue('A1','姓名')->setCellValue('B1','工具');//给当前活动sheet填充数据，数据填充是按顺序一行一行填充的，假如想给A1留空，可以直接setCellValue(‘A1’,’’);
        $PHPSheet->setCellValue('C1','物件')->setCellValue('D1','数值');
        $PHPSheet->setCellValue('E1','图片')->setCellValue('F1','时间');
      

        $experiment=model('index/Experiment');
        $dataList=$experiment->getRecord();
        foreach($dataList as $key=>$value){
            $key2=0;
            $key2=$key+2;
           // $PHPSheet->getRowDimension($key2)->setRowHeight(50);
            $PHPSheet->setCellValue('A'.$key2,$value['surveyor'])->setCellValue('B'.$key2,$value['tool']);
            $PHPSheet->setCellValue('C'.$key2,$value['thing'])->setCellValue('D'.$key2,$value['testValue']);
			if(!empty($value['imageUrl'])){
				$PHPSheet->setCellValue('E'.$key2,'http://'.$_SERVER['HTTP_HOST'].'/'.$value['imageUrl']);
			}
			
            $PHPSheet->setCellValue('F'.$key2,date('Y-m-d H:i',$value['addTime']));
          /* if(!empty($value['imageUrl'])){
            $objDrawing = new \PHPExcel_Worksheet_Drawing();
			$objDrawing->setResizeProportional(false);
            $objDrawing->setName('Photo');
            $objDrawing->setDescription('Photo');
            $objDrawing->setPath($value['imageUrl']); //图片文件
            $objDrawing->setHeight(60); //图片高
            $objDrawing->setWidth(120); //图片宽
            $objDrawing->setCoordinates('E'.$key2); //图片左上角所在单元格
            $objDrawing->setWorksheet($PHPExcel->getActiveSheet()); //放到工作簿中
           }*/
       
        }
        // dump($dataList);
        // die;
       

       
       // $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007');//按照指定格式生成Excel文件，‘Excel2007’表示生成2007版本的xlsx，‘Excel5’表示生成2003版本Excel文件
  
		 header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="数字化测量数据.xlsx"');
		 //header('Content-Type: application/vnd.ms-excel');//告诉浏览器输出07Excel文件
		// Response.ContentType="application/vnd.ms-excel";
		// header('Content-Type: application/x-xls');  
      //header('Content-Type:application/vnd.ms-excel');//告诉浏览器将要输出Excel03版本文件
       // header('Content-Disposition: attachment;filename="数字化测量数据.xlsx"');
	    ob_clean(); 
		$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007');//按照指定格式生成Excel文件，‘Excel2007’表示生成2007版本的xlsx，‘Excel5’表示生成2003版本Excel文件
             
			  header('Cache-Control: max-age=0');//禁止缓存
             // $PHPWriter->save("php://output");
			 $PHPWriter->save("数字化测量数据.xlsx");
			header('location:/数字化测量数据.xlsx');
			//echo 1;
			//die;
    }

}

